package net.hcpme.activity;

import java.util.List;

import net.hcpme.dao.Message;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.BaseAdapter;
import android.widget.TextView;

// 设置启动javaScript
@SuppressLint("SetJavaScriptEnabled")
public class ChatRoomAdapter extends BaseAdapter {

    private Context context;
    private List<Message> messages;
    private WebView msgView;

    public ChatRoomAdapter(Context context, List<Message> messages) {
        this.context = context;
        this.messages = messages;
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message message = messages.get(position);
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (ChatRoom.MESSAGE_TYPE_IN == message.getInOrOut()) {
            convertView = inflater.inflate(R.layout.chat_in, null);
        } else {
            convertView = inflater.inflate(R.layout.chat_out, null);
        }
        TextView useridView = (TextView) convertView.findViewById(R.id.formclient_row_userid);
        TextView dateView = (TextView) convertView.findViewById(R.id.formclient_row_date);
        msgView = (WebView) convertView.findViewById(R.id.formclient_row_msg);
        WebSettings webSettings = msgView.getSettings(); // webView: 类WebView的实例
        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); // 就是这句
        webSettings.setJavaScriptEnabled(true);// 启用javaScript
        initWebView(message.getMessage());
        useridView.setText(message.getFrom());
        dateView.setText(message.getDate());
        return convertView;
    }

    private void initWebView(String url) {
        // 添加js交互接口类，并起别名 imagelistner
        msgView.addJavascriptInterface(new JavascriptInterface(context), "imagelistner");
        msgView.setWebViewClient(new MyWebViewClient());

        msgView.loadDataWithBaseURL("", url, "text/html", "utf-8", null);
    }

    // js通信接口
    public class JavascriptInterface {
        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }

        public void openImage(String img) {
            System.out.println(img);
            //
            Intent intent = new Intent();
            intent.putExtra("image", img);
            intent.setClass(context, ShowWebImageActivity.class);
            context.startActivity(intent);
            System.out.println(img);
        }
    }

    // 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            super.onPageFinished(view, url);
            // html加载完成之后，添加监听图片的点击js函数
            addImageClickListner();

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);

        }
    }

    // 注入js函数监听
    private void addImageClickListner() {
        // 这段js函数的功能就是，遍历所有的img几点，并添加onclick函数，函数的功能是在图片点击的时候调用本地java接口并传递url过去
        msgView.loadUrl("javascript:(function(){" + "var objs = document.getElementsByTagName(\"img\"); " + "for(var i=0;i<objs.length;i++)  " + "{"
                + "    objs[i].onclick=function()  " + "    {  " + "        window.imagelistner.openImage(this.src);  " + "    }  " + "}" + "})()");
    }

}
